/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.ui.controls;

import com.easoft.code.pos.ui.leftoptions.ComponentBuilder;
import com.easoft.code.pos.util.UIUtil;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Eliu
 */
public class ButtonMenu extends JButton {

    private JPanel destination;
    private ComponentBuilder builder;
    private Component defaultComponent;

    public ButtonMenu(String label, String pic, JPanel destination, Component defaultComponent) {
        super(label);
        this.setIcon(UIUtil.getImage(pic));
        this.destination = destination;
        this.defaultComponent = defaultComponent;
        addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                click(evt);
            }
        });
    }

    public ButtonMenu(String label, String pic, JPanel dest, ComponentBuilder builder) {
        super(label);
        this.setIcon(UIUtil.getImage(pic));
        this.destination = dest;
        this.builder = builder;
        addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                click(evt);
            }
        });
    }

    private void click(java.awt.event.ActionEvent evt) {
        destination.removeAll();
        if (defaultComponent != null) {
            defaultComponent.setPreferredSize(new Dimension(600, 400));
            destination.add(defaultComponent);
        } else {
            destination.add(builder.build());
        }
        destination.revalidate();
        destination.repaint();
        if (defaultComponent != null) {
            ((PreRenderControl) defaultComponent).refresh();
        }
    }
}
